﻿using System;
using System.Windows;
using System.Reflection;
using WpfLob.Frame;

namespace WpfLob
{
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
            RegisterAllPanes();
		}

        private void RegisterAllPanes()
        {
            // Find all pane view model types in the assembly.
            Assembly assembly = Assembly.GetExecutingAssembly();
            foreach (Type viewModelType in assembly.GetTypes())
            {
                if (typeof(Pane).IsAssignableFrom(viewModelType) && viewModelType.Name.EndsWith("ViewModel"))
                {
                    string viewTypeName = viewModelType.FullName.Replace("ViewModel", "View");
                    Type viewType = assembly.GetType(viewTypeName);
                    if (viewType != null)
                        RegisterDataTemplate(viewModelType, viewType);
                }
            }
        }

        private void RegisterDataTemplate(Type viewModelType, Type viewType)
        {
            DataTemplate dataTemplate = new DataTemplate(viewModelType)
            {
                VisualTree = new FrameworkElementFactory(viewType)
            };
            Resources.Add(dataTemplate.DataTemplateKey, dataTemplate);
        }
    }
}